﻿using GetData.webProductClass;
using jutehui.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace GetData
{
    /// <summary>
    /// 买个便宜货 解析类
    /// </summary>
    public class mgpyhClass
    {

        #region 定义商品分类
        /// <summary>
        /// 定义买个便宜货商品分类 初始化
        /// </summary>
        public static Dictionary<string, List<string>> mgpyhCategory = new Dictionary<string, List<string>>();
        public Dictionary<string, List<string>> GetCategory()
        {
            //定义静态类 买个便宜货 商品分类  
            string detailCategory = "Kindle,纸质图书,在线教育,蓝光,CD,游戏";
            var list = detailCategory.Split(',').ToList();
            mgpyhCategory.Add("图书音像", list);

            detailCategory = @"智能家居,健康监测,智能眼镜,智能手表,智能手环,贴膜保护壳,移动电源,电池,充电器,存储卡U盘,运营商,网络盒子,网络存储器,交换机,网卡,路由器   电影票,音频设备,音箱音响,耳机耳麦,摄影器材,镜头,滤镜,闪光灯,摄像机,运动相机,数码相机,手机";
            list = detailCategory.Split(',').ToList();
            mgpyhCategory.Add("手机数码", list);

            detailCategory = @"散热器,声卡,光驱,显示器,电源,机箱,内存,硬盘固态硬盘,显卡,主板,CPU,摄像头,电脑工具,游戏控制器,UPS,
手写板数位板,线缆,鼠标垫,移动硬盘,键盘,鼠标,服务器工作站,电玩,平板电脑,笔记本,台式机,财会用品,画具画材,笔,计算器本册便签,文具管理,学生文具,纸张,魔盒,硒鼓墨粉,安防监控,打印机,保险柜,会议音频视频,POS机,考勤机,碎纸机,验钞机,传真机,打印机一体机";
            list = detailCategory.Split(',').ToList();
            mgpyhCategory.Add("电脑办公", list);

            detailCategory = "其他乐器配件,口琴,吉他,钢琴";
            list = detailCategory.Split(',').ToList();
            mgpyhCategory.Add("玩模乐器", list);

            detailCategory = @"其他健康电器,计步器/脂肪检测仪,体温计,健康秤/厨房秤,足浴盆,按摩椅按摩器,理发器卷/直发器,美容器,
电吹风,剃/脱毛器,饮水机,其它生活电器,电风扇,收录音机,除湿机干衣机,电话机,插座,熨挂烫机,吸尘器,扫地机器人,净化器加湿器,
取暖电器,其他厨房电器,电炖锅,电水壶/热水瓶,酸奶机,煮蛋器,电饼铛/烧烤盘,电磁炉,电烤箱,料理榨汁机,微波炉,咖啡机,面包机,豆浆机,
电饭煲,压力锅,投影仪,洗碗机,消毒柜。热水器,烟机灶具,迷你音响,家庭影院,空调冰箱洗衣机,平板电视";
            list = detailCategory.Split(',').ToList();
            mgpyhCategory.Add("家用电器", list);

            detailCategory = @"监控安防,电工电料,电气开关插座,厨卫五金,龙头花洒,水槽,洁身器,灯具LED灯,浴霸/排气扇,仪器仪表,
手动工具,电动工具,热水袋,雨伞,洗晒烫,缝纫用品,浴室用品,收纳清洁用品,户外家具,椅凳,餐桌,电视柜,沙发,床,衣柜,书柜书架,电脑椅,
电脑桌,鞋柜,置物收纳架,水族用品,小宠用品,猫用品,狗用品,园艺,净水,墙地面材料,花瓶摆件,装饰画相框,窗帘,凉席,蚊帐,电热毯,
毛巾浴巾毛毯,抱枕,床垫,枕头,被子被套,床单件套,酒具,烘焙烧烤用具,保温杯壶,厨房收纳,厨房小工具,刀剪具,锅碗瓢盆";
            list = detailCategory.Split(',').ToList();
            mgpyhCategory.Add("家居家装", list);

            detailCategory = @"肉类,海产品,水果蔬菜,芝麻糊,蜂蜜,柚子茶,奶茶,麦片,茶叶,调味品,干货,杂粮,方便食品,食用油,米,饮料,其他洋酒,酒精饮料,啤酒,起泡酒,葡萄酒,白酒 耐缺氧,抗氧化,骨骼健康,明目益智,肠胃养护,肝肾养护,美容养颜,
美体塑身,缓解疲劳,调节三高,调节免疫  橄榄油,意面方便面,零食,饼干,咖啡,牛奶";
            list = detailCategory.Split(',').ToList();
            mgpyhCategory.Add("食品饮料", list);

            detailCategory = @"驱蚊杀虫,卫生巾,衣物清洁,老花镜,太阳镜,隐形眼镜,眼镜,中医保健,除颤器,制氧机,助听器,血糖仪,血压计,保健护具,成人用品,按摩器械,
按摩椅,口罩,情趣用品,香水彩妆,女性护理,身体护肤,洗发护发,面部护肤,口腔护理";
            list = detailCategory.Split(',').ToList();
            mgpyhCategory.Add("个护化妆", list);


            detailCategory = @"孕妇装,产后内衣,产后塑身服,童鞋,内外服饰,婴儿背带,电动车,学步车,餐椅摇椅,婴儿推车,
安全座椅,防溢乳垫,吸乳器,清洁,护肤,洗浴,餐具,奶瓶奶嘴,肉松,鱼油,清火开胃,钙铁锌,DHA,饮品,米粉汤粥,布尿裤,成长裤,
XXL,XL,L,M,NB/S,成人奶粉,孕产奶粉,特殊配方,4段,3段,2段,1段,儿童监护,乐器发声,益智玩具,动漫相关,健身玩具,模型玩具,毛绒布艺,LEGO积木拼插";
            list = detailCategory.Split(',').ToList();
            mgpyhCategory.Add("母婴玩具", list);

            detailCategory = "其他奢侈品,金银饰品,钻石 ,机械,石英";
            list = detailCategory.Split(',').ToList();
            mgpyhCategory.Add("钟表首饰", list);

            detailCategory = @"上门保养,整车销售,胎压监控,车载冰箱,倒车雷达,导航仪,行车记录仪,汽修工具,汽车贴膜,蓄电池,刹车,轮胎,
火花塞,防冻液,机油,滤清器,车灯,雨刷,轮毂";
            list = detailCategory.Split(',').ToList();
            mgpyhCategory.Add("汽车用品", list);

            detailCategory = @"瑜伽舞蹈,运动护具,哑铃,健身车,跑步机,车辆附件,头盔,骑行服,通勤车,山地车,折叠车,高尔夫,网球,乒乓球,篮球,
            羽毛球,游泳,足球,凉鞋,溯溪鞋,越野跑鞋,登山鞋,徒步鞋,压缩衣,户外刀,心率表,户外背包,水壶,望远镜,烧烤,吊床,睡袋,帐篷,钓鱼,户外袜等,
帽子手套,冲锋裤,皮肤风衣,软壳,抓绒,冲锋衣";
            list = detailCategory.Split(',').ToList();
            mgpyhCategory.Add("运动户外", list);

            detailCategory = @"品牌女包,挎包,数码配件包,钱包,妈咪包,书包,登山包,相机包,旅行包,拉杆箱 跑步鞋,居家鞋,时装靴,靴子,高跟鞋,运动鞋,
帆布鞋,正装鞋,休闲鞋,配饰,围巾,手套,卫衣,牛仔裤,衬衫,休闲裤,毛衣针织衫,西装,夹克,羽绒服,内衣,大衣,连衣裙";
            list = detailCategory.Split(',').ToList();
            mgpyhCategory.Add("服饰鞋包", list);

            return mgpyhCategory;
        }
        #endregion
        HtmlAgilityPack.HtmlDocument doc;
        public mgpyhClass()
        {
            doc = new HtmlAgilityPack.HtmlDocument();
            GetCategory();
        }

        /// <summary>
        /// 通过网页html获取商品信息并保存到数据库中
        /// </summary>
        /// <param name="url"></param>
        /// <param name="shoppingType">海淘2 精选1</param>
        /// <returns></returns>
        public string GetProductsToDataBase(string url)
        {
            //  string url = "http://www.shihuo.cn/";
            var htmlText = CustomMethod.GetHtmlByHttpHelper(url);
            string msg = "";

            var pList = GetProductByRegex(htmlText);
            if (pList == null)
            {
                return "";
            }
            DataTable dt = CustomMethod.ListToDataTable<Product>(pList, "Product", ref msg);
            if (dt == null || dt.Rows.Count < 1)
            {
                return "";
            }
            jutehui.BLL.Product bll = new jutehui.BLL.Product();
            if (bll.SaveTableData("Product", dt, out msg))
            {
                return dt.Rows.Count.ToString(); ;
            }
            else
            {
                msg = "识货：GetProductsToDataBase" + msg;
                CustomMethod.WriteLog(msg);
                return "";

            }
        }
        /// <summary>
        /// 根据正则解析商品信息
        /// </summary>
        /// <param name="htmlText"></param>
        /// <param name="type">海购还是精选</param>
        /// <returns></returns>
        public List<Product> GetProductByRegex(string htmlText)
        {
            List<Product> pList = new List<Product>();
            doc.LoadHtml(htmlText);

           // string productRegex = @"<div class=""item-inner"">[\s\S]*?<div class=""special-tag ""></div>";
            var collect = doc.DocumentNode.SelectNodes("//li[@class='content-item']");
            if(collect==null||collect.Count==0)
            {
                CustomMethod.WriteLog("买个便宜货：GetProductByRegex 网站没有解析出商品信息，请检查规则。");
                return null;
            }
            List<string> list = new List<string>();
            for(int index=0;index<collect.Count;index++)
            {
                list.Add(collect[index].InnerHtml);
            } 
           
            ProductTypeEnum pEnum = new ProductTypeEnum();
            for (int index = 0; index < list.Count; index++)
            {
                Product product = new Product();
                try
                {
                    product = GetProduct(list[index]);
                    if(product==null)
                    {
                        continue;
                    }
                    ////------------------初始化数据----------------------------
                    //product.FromWeb = "买个便宜货";
                    //product.UpdateTime = DateTime.Now;
                    //product.UpdateTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    ////------------------------------------------------------------------------------
                    //var herLink = CustomMethod.GetInfoFroma(list[index]);
                    //if (herLink == null || herLink.Count < 1)
                    //{
                    //    CustomMethod.WriteLog("买个便宜货：GetProductByRegex，没有获取商品链接区域");
                    //    continue;
                    //}
                    //string buyUrl = "";
                    //for (int linkIndex = 0; linkIndex < herLink.Count; linkIndex++)
                    //{
                    //    if (herLink[linkIndex] == "直达链接")
                    //    {
                    //        buyUrl = herLink[linkIndex - 1];
                    //    }
                    //}
                    //string currentUrl = "http://www.mgpyh.com/" + buyUrl;
                    //string productUrl = "";
                    //product.Plink = CustomMethod.GetTrueUrl(currentUrl, ref productUrl);
                    //product.Memo = productUrl;
                    //if (product.Plink == "")
                    //{
                    //    if (productUrl == "")
                    //    {
                    //        CustomMethod.WriteLog("买个便宜货：GetProductByRegex，没有获取商品链接");
                    //    }
                    //    continue;
                    //}
                    ////来源 图标
                    ////var sourceLogo = CustomMethod.GetImgUrl(list[index],"买个便宜货");
                    ////if (sourceLogo != "")
                    ////{
                    //product.Source = ""; // sourceLogo;
                    //// }
                    ////图片链接
                    //string srcRegex = @"data-original=[""|']([^""|']*)[""|']";
                    //var srcImg = CustomMethod.GetInfoByRegex(srcRegex, list[index], 1);
                    //if (srcImg.Count > 0)
                    //{
                    //    product.ImgUrl = srcImg[0].Split('!').ToList()[0];

                    //}
                    ////获取详细页面 正则匹配 标题 描述信息 价格
                    //string detailConotenRegex = @"<div class=""prod-img"">[\s\S]*?</div>";
                    //var detailLastUrlContent = CustomMethod.GetInfoByRegex(detailConotenRegex, list[index], 0);
                    //if (detailLastUrlContent == null || detailLastUrlContent.Count < 1)
                    //{
                    //    CustomMethod.WriteLog("买个便宜货：detailLastUrlContent，没有获取跳转详细页面链接区域");
                    //    continue;
                    //}
                    //var detailList = CustomMethod.GetInfoFroma(detailLastUrlContent[0]);
                    //if (detailList == null || detailList.Count < 1)
                    //{
                    //    CustomMethod.WriteLog("买个便宜货：detailList，没有获取跳转详细页面链接");
                    //    continue;
                    //}
                    //string detailUrl = "http://www.mgpyh.com/" + detailList[0];
                    //string detailHtml = CustomMethod.GetHtmlByHttpHelper(detailUrl);
                    //string titRegex = @" <h1 class=""title"">(.*?)</h1>";
                    //var titlList = CustomMethod.GetInfoByRegex(titRegex, detailHtml, 1);
                    //if (titlList == null || titlList.Count < 1)
                    //{ 
                    //    titRegex = @"<title>(.*?)</title>";
                    //    titlList = CustomMethod.GetInfoByRegex(titRegex, detailHtml, 1);
                    //    if (titlList == null || titlList.Count < 1)
                    //    {
                    //        CustomMethod.WriteLog("买个便宜货：GetProductByRegex，没有获取商品标题，价格");
                    //        continue;
                    //    }
                    //}
                    ////ECCO Biom Lite Slip-On 健步轻巧系列 4.5折 $57.98<em class="number"> $57.98</em>
                    //// <title>爱步（ECCO） Saunter Leather Chelsea   Saunter 女式真皮切尔西靴 $97 需用码  - 买个便宜货</title>
                    //var detailInfo = Regex.Replace(titlList[0], @"<(.[^>]*)>", "|");
                    //var titlAndPri = detailInfo.Split('|').ToList();
                    //if (titlAndPri.Count > 2)
                    //{
                    //    product.Pname = titlAndPri[0];
                    //    product.Price = titlAndPri[titlAndPri.Count - 2];
                    //    if (product.Price.Contains("￥"))
                    //    {
                    //        product.TypeId = 1;
                    //    }
                    //    else
                    //    {
                    //        product.TypeId = 2;
                    //    }
                    //}
                    //else
                    //{
                    //    product.Pname = titlAndPri[0].Replace("- 买个便宜货", "");
                    //    if (product.Pname.Contains("￥"))
                    //    {
                    //        product.TypeId = 1;
                    //    }
                    //    else { product.TypeId = 2; }
                    //}
                    //string regex = @"<meta content=(.*?) name=""keywords"" />";
                    //var categoryContent = CustomMethod.GetInfoByRegex(regex, detailHtml, 1);
                    //if (categoryContent.Count > 0)
                    //{
                    //    var category = categoryContent[0].Replace("&#x2F;", "/");

                    //    foreach (KeyValuePair<string, List<string>> kv in mgpyhCategory)
                    //    {
                    //        var valuesList = (List<string>)kv.Value;
                    //        for (int count = 0; count < valuesList.Count; count++)
                    //        {
                    //            if (category.Contains(valuesList[count].Trim()))
                    //            {
                    //                var itme = Enum.Parse(typeof(ProductTypeEnum.mgpyhType), kv.Key);
                    //                product.ProductTypeId = Convert.ToInt64(itme);
                    //                break;
                    //            }
                    //        }
                    //    }
                    //    if (product.ProductTypeId == null)
                    //    {
                    //        product.ProductTypeId = 16;
                    //    }
                    //}
                    //else
                    //{
                    //    product.ProductTypeId = 16;
                    //}

                   
                    //product.Pname = System.Web.HttpUtility.HtmlEncode(product.Pname);
                   


                }
                catch (Exception ex)
                {
                    CustomMethod.WriteLog("买个便宜货：GetProductByRegex:" + ex.Message.ToString());
                    continue;
                }
                pList.Add(product);

            }
            var newList = CustomMethod.GetDistinctProdcuts(pList);
            newList.Reverse();
            return newList;
        }
        /// <summary>
        ///根据规则获取属性值 一个商品item
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <returns></returns>
        public Product GetProduct(string htmlContent)
        {
            Product pro = new Product();
            pro.FromWeb = "买个便宜货";
            pro.UpdateTime = DateTime.Now;
            pro.UpdateTimeStr = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            pro.TypeId = 1;
            doc.LoadHtml(htmlContent);
            //名称
            var name = CustomMethod.GetValue(doc, "//h1");
            if (name == "")
            {
                return null;
            }
            pro.Pname = name.Trim().Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "").Replace("&nbsp;","");
            //价格
            var pric = CustomMethod.GetValue(doc, "//em[@class='number']");
            pro.Price = pric.Trim().Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "").Replace("&nbsp;", ""); ;
            //图片
            var imglis = CustomMethod.GetAttr(doc, htmlContent, "//a/img", "src");
            if (imglis != null && imglis.Count > 0)
            {
                pro.ImgUrl = imglis[0];
            } 
            //链接
            var outlink = CustomMethod.GetAttr(doc, htmlContent, "//a[@class='zhida']", "href");
            if (outlink != null && outlink.Count > 0)
            {
                string productUrl = "";
                string link = "http://www.mgpyh.com/"+outlink[0];
                //商品链接 带渠道号 
                pro.Plink = CustomMethod.GetTrueUrl(link, ref productUrl);
                if (pro.Plink == "")
                {
                    if (productUrl == "")
                    {
                        CustomMethod.WriteLog("买个便宜货：没有抓取到商品链接");

                    }
                    return null;
                }
                //入库前匹配数据库是否存在该商品 商品来源是否重复标识
                pro.Memo = productUrl;
            }
            else
            {
                return null;
            }
            //详细信息
            var detaillink = CustomMethod.GetAttr(doc, htmlContent, "//a", "href");
            if (detaillink != null && detaillink.Count > 0)
            {
                var detail = "http://www.mgpyh.com/" + detaillink[0];
                string htmlDescription = CustomMethod.GetHtmlByHttpHelper(detail);
                doc.LoadHtml(htmlDescription);//详细内容 

                var item = doc.DocumentNode.SelectNodes("//div[@class='post-content img-max-size']/p");
                string descr = "";
                if (item != null && item.Count > 0)
                {
                    for (int index = 0; index < item.Count; index++)
                    {
                        string des = item[index].InnerText;
                        if(string.IsNullOrEmpty(des))
                        {
                            continue;
                        }
                        descr += "<p>" + des + "</p><br/>";
                    }
                }
                pro.Description = descr;
                if (descr == "")
                {
                    pro.Description = pro.Pname;
                }
                //大图
                var bigTu = CustomMethod.GetAttr(doc, htmlDescription, "//div[@class='post-content img-max-size']/p/img", "src");

                if (bigTu != null && bigTu.Count > 0)
                {
                    pro.ImgUrl2 = bigTu[0];
                }
                //数据源
                var sourcolle = doc.DocumentNode.SelectNodes("//ul[@class='info']/li/span");
                if (sourcolle != null && sourcolle.Count > 0)
                {
                    pro.Source = sourcolle[sourcolle.Count-1].InnerText;
                }

                
                //分类  
                doc.LoadHtml(htmlDescription);
                var categoryContent = CustomMethod.GetValue(doc, "//ul[@class='info']/li/span/a");
                if (categoryContent!="")
                {
                    var category = categoryContent.Replace("&#x2F;", "/");

                    foreach (KeyValuePair<string, List<string>> kv in mgpyhCategory)
                    {
                        var valuesList = (List<string>)kv.Value;
                        for (int count = 0; count < valuesList.Count; count++)
                        {
                            if (category.Contains(valuesList[count].Trim()))
                            {
                                var itme = Enum.Parse(typeof(ProductTypeEnum.mgpyhType), kv.Key);
                                pro.ProductTypeId = Convert.ToInt64(itme);
                                break;
                            }
                        }
                    }
                    if (pro.ProductTypeId == null)
                    {
                        pro.ProductTypeId = 16;
                    }
                }
                else
                {
                    pro.ProductTypeId = 16;
                }
            }
            return pro;
        }
    }
}
